import { computed, ref } from 'vue';
import { TableColumnType } from 'ant-design-vue';
import { defineStore } from 'pinia';
import { useRoute } from 'vue-router';
import { TABLE_COLUMNS_STORE } from '../constant';

/**
 * 用于 ProTableColumns 自定义列持久化存储
 */
export const useTableColumnsStore = defineStore(
  TABLE_COLUMNS_STORE,
  () => {
    const route = useRoute();
    const stores = ref<Record<string, TableColumnType[]>>({});
    const currentColumns = computed(() => {
      return stores.value[route.path];
    });
    const updateColumns = (columns: TableColumnType[]) => {
      stores.value[route.path] = columns;
    };
    return {
      currentColumns,
      updateColumns,
      stores,
    };
  },
  {
    persist: {
      storage: localStorage,
    },
  },
);
